package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.orders.pojo.Customer;
import com.orders.pojo.User;

public class CustomerOperation implements Operation {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	private Customer customer;
	@Override
	public boolean insert(Object obj) {
		try {
			if(obj instanceof Customer){
				customer = (Customer)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("insert into customer values (?,?,?,?,?,?)");
				ps.setInt(1, customer.getId());
				ps.setString(2, customer.getUsername());
				ps.setString(3, customer.getPassword());
				ps.setString(4, customer.getStatus());
				ps.setInt(5, 0);
				ps.setBoolean(6, customer.isVisible());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public boolean update(Object obj) {
		try {
			if(obj instanceof Customer){
				customer = (Customer)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update customer set username = ?,password = ?,status = ?,person_information_id = ?,visible = ? where id = ?");
				ps.setString(1, customer.getUsername());
				ps.setString(2, customer.getPassword());
				ps.setString(3, customer.getStatus());
				ps.setInt(4, 0);
				ps.setBoolean(5, customer.isVisible());
				ps.setInt(6, customer.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

	@Override
	public List select(Object obj) {
		List list = new ArrayList<>();
		try {
			if(obj instanceof Customer){
				customer = (Customer)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from customer where id =? and visible = 1");
				ps.setInt(1, customer.getId());
				rs = ps.executeQuery();
				while(rs.next()){
					customer.setId(rs.getInt("id"));
					customer.setUsername(rs.getString("username"));
					customer.setPassword(rs.getString("password"));
					customer.setPi(null);
					customer.setStatus(rs.getString("status"));
					list.add(customer);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return list;
	}
	
	public Customer selectById(int id) {
		try {
				customer = new Customer();
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from customer where id =? and visible = 1");
				ps.setInt(1, id);
				rs = ps.executeQuery();
				rs.next();
				customer.setId(rs.getInt("id"));
				customer.setUsername(rs.getString("username"));
				customer.setPassword(rs.getString("password"));
				customer.setPi(null);
				customer.setStatus(rs.getString("status"));	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return customer;
	}
	
	public Customer selectByUsername(String username) {

		try {
				customer = new Customer();
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("select * from customer where username = ? and visible = 1");
				ps.setString(1, username);
				rs = ps.executeQuery();
				rs.next();
					customer.setId(rs.getInt("id"));
					customer.setUsername(rs.getString("username"));
					customer.setPassword(rs.getString("password"));
					customer.setPi(null);
					customer.setStatus(rs.getString("status"));		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return customer;
	}

	@Override
	public boolean delete(Object obj) {
		try {
			if(obj instanceof Customer){
				customer = (Customer)obj;
				conn = ConnectionFactory.getConnection();
				ps = conn.prepareStatement("update customer set visible = ? where id = ?");
				ps.setBoolean(1, false);
				ps.setInt(2, customer.getId());
				return !ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     return false;
	}

}
